<?php

/**
 * Description of VirtualLabs_Admin_Machines
 *
 * @author markos
 */
class VirtualLabs_Admin_Capacity extends VirtualLabs_Admin_Dao {

    const TABLE_NAME = "configuracion_encendido_global";

    public function __construct() {
        
    }

    public function getAllConfigCapacity() {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()->from(VirtualLabs_Admin_Capacity::TABLE_NAME, '*');
            return $db->fetchAll($select);
        } catch (Exception $e) {
            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debugLog->debug($e->getMessage());
            $debugLog->debug($e->getTraceAsString());
            throw $e;
        }
    }

    public function getConfigCapacityByCodigo($codigoCapacity) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()->from(VirtualLabs_Admin_Capacity::TABLE_NAME, '*')
                    ->where("codigo_configuracion_encendido_global = ?", $codigoCapacity);
            return $db->fetchRow($select);
        } catch (Exception $e) {
            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debugLog->debug($e->getMessage());
            $debugLog->debug($e->getTraceAsString());
            throw $e;
        }
    }

    /**
     * 
     * @param type $hora_inicio
     * @param type $hora_fin
     * @param type $dia
     */
    public function validateHours($hora_inicio, $hora_fin, $dia, $codigo) {
        try {
            $db = Zend_Db_Table::getDefaultAdapter();
            $whereSQL = "dia = {$dia} AND " .
                    "( ('{$hora_inicio}' BETWEEN hora_inicio AND hora_fin) OR ('{$hora_fin}' BETWEEN hora_inicio AND hora_fin) " .
                    "OR (hora_inicio BETWEEN '{$hora_inicio}' AND '{$hora_fin}') OR (hora_fin BETWEEN '{$hora_inicio}' AND '{$hora_fin}'))";
            if ($codigo != 'new') {
                $whereSQL.= " AND codigo_configuracion_encendido_global NOT IN ({$codigo})";
            }
            $select = $db->select()->from(VirtualLabs_Admin_Capacity::TABLE_NAME, 'count(*) as cantidad')
                    ->where($whereSQL);

            $result = $db->fetchAll($select);

            if ($result[0]['cantidad'] > 0) {
                return FALSE;
            } else {
                return TRUE;
            }
        } catch (Exception $e) {
            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debugLog->debug($e->getMessage());
            $debugLog->debug($e->getTraceAsString());
            throw $e;
        }
    }

    public function savecapacity($codigo, $hora_inicio, $hora_fin, $dia, $cpu, $mem) {
        try {
            $db = Zend_Db_Table::getDefaultAdapter();
            $data = array();
            $data['hora_inicio'] = $hora_inicio;
            $data['hora_fin'] = $hora_fin;
            $data['dia'] = $dia;
            $data['cantidad_cpu'] = $cpu;
            $data['cantidad_ram'] = $mem;
            $data['estado'] = 1;
            if ($codigo == 'new') {
                $db->insert(VirtualLabs_Admin_Capacity::TABLE_NAME, $data);
            } else {
                $codigo_configuracion = array("codigo_configuracion_encendido_global = ?" => $codigo);
                $db->update(VirtualLabs_Admin_Capacity::TABLE_NAME, $data, $codigo_configuracion);
            }
        } catch (Exception $e) {
            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debugLog->debug($e->getMessage());
            $debugLog->debug($e->getTraceAsString());
            throw $e;
        }
    }

    public function deletecapacityById($codigo) {
        try {
            $db = Zend_Db_Table::getDefaultAdapter();
            $db->beginTransaction();
            $codigo_configuracion = array("codigo_configuracion_encendido_global = ?" => $codigo);
            $db->delete(VirtualLabs_Admin_Capacity::TABLE_NAME, $codigo_configuracion);
            $db->commit();
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

    /*
     * Esta funcion obtiene el la configuracion de capacidad que conincide con el horario actual.
     */

    public function getConfigCapacityNow() {
        try {
            
            $dayWeek = array(
                'Mon' => 1,
                'Tue' => 2,
                'Wed' => 3,
                'Thu' => 4,
                'Fri' => 5,
                'Sat' => 6,
                'Sun' => 7
            );
            
            $dia = $dayWeek[date('D')];            
            $hora_actual = date("H:i:s");            
//            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
//            $debugLog->debug("dia {$dia} hora {$hora_actual}");
//            $dia = 1;            
//            $hora_actual = "13:00:00";                        
            $db = Zend_Db_Table::getDefaultAdapter();            
            $whereSQL = "dia = {$dia} AND " . "('{$hora_actual}' BETWEEN hora_inicio AND hora_fin)";            
            $select = $db->select()->from(VirtualLabs_Admin_Capacity::TABLE_NAME, '*')
                    ->where($whereSQL);
//            $debugLog->debug("select: " . $select);
            $result = $db->fetchRow($select);            
//            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
//            $debugLog->debug("Resultado: " . print_r($result,TRUE)); 
//            if(is_array($result)) {
//                $debugLog->debug("Isarray"); 
//            } else {
//                $debugLog->debug("Is not array"); 
//            }
//            
//            $debugLog->debug("Select: " . $select);             
            return $result;
        } catch (Exception $e) {
            $debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debugLog->debug($e->getMessage());
            $debugLog->debug($e->getTraceAsString());
            throw $e;
        }
    }

}

?>
